package com.mianshi;

public class 内存可见性 {


//    private static volatile boolean flag = false; //加了volatile没问题

    private static boolean flag = false; //不加的话，循环中不输出则永远结束不了

    static class Test extends Thread {

        @Override
        public void run() {
            while (!flag) {
//                System.out.flush();
            }
        }
    }

    public static void main(String[] args) {
        new Thread(() -> {System.out.println("a");}).start();
        Test m = new Test();
        m.start();
        try {
            Thread.sleep(200);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        flag = true;
        try {
            m.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("done");
    }

}
